package com.njym.java.security.app.rest;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.njym.java.security.app.biz.ContactRecordBiz;
import com.njym.java.security.app.entity.ContactRecord;
import com.njym.java.security.common.msg.TableResultResponse;
import com.njym.java.security.common.rest.BaseController;
import com.njym.java.security.common.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;

@Controller
@RequestMapping("contactRecord")
public class ContactRecordController extends BaseController<ContactRecordBiz, ContactRecord> {

    @Autowired
    private ContactRecordBiz contactRecordBiz;

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ResponseBody
    public Object lists(@RequestParam(defaultValue = "10") int limit, @RequestParam(defaultValue = "1")int page,
                                     String fid, String netUserId){
        if (!fid.equals("undefined") && !netUserId.equals("undefined")) {
            Page<Object> result;
            Example example = new Example(ContactRecord.class);
            example.createCriteria().andEqualTo("fid",Integer.parseInt(fid)).andEqualTo("netUserId",Integer.parseInt(netUserId));
            example.orderBy("contTime").desc();
            result = PageHelper.startPage(page, limit);
            baseBiz.selectByExample(example);
            return new TableResultResponse<>(result.getPages(),result.getResult());
        } else {
            return ResponseUtil.fail(40301, "错误！");
        }

    }

    /**
     * 新建交往
     * @param netUserId
     * @param fid
     * @param contContent
     * @param contSite
     * @return
     */
    @RequestMapping(value = "/addContactRecord",method = RequestMethod.GET)
    @ResponseBody
    public Object create(String netUserId,String fid,String contContent,String contSite){
        if (!fid.equals("undefined") && !netUserId.equals("undefined")) {
            ContactRecord contactRecord = new ContactRecord();
            contactRecord.setNetUserId(Integer.parseInt(netUserId));
            contactRecord.setFid(Integer.parseInt(fid));
            contactRecord.setContContent(contContent);
            contactRecord.setContSite(contSite);
            contactRecord.setContTime(new Date());
            baseBiz.insert(contactRecord);
            return  ResponseUtil.ok();
        } else {
            return ResponseUtil.fail(40301, "错误！");
        }

    }

}